<?php
    class admin_model_menu extends model
    {
        public $id = '';

        public function __construct()
        {
            parent::__construct();
            $this->table_name = config::get()->table->menu;
        }

        /**
         * 获取管理员菜单列表
         * @param $group_id
         */
        public function get_menu_list($group_id)
        {
            $permissions = $this->get_user_permissions($group_id);

            $menu_list   = $this->get_all_menu();

            $menu_list   = $this->sort_menu($menu_list);

            return array('permissions' => $permissions,
                         'menu_list'   => $this->through_user_permissions_get_list($permissions, $menu_list));
        }

        /**
         * 获取所有菜单
         */
        public function get_all_menu()
        {
            $sql = "select * from `{$this->table_name}`";
            $menu_list = $this->db->query($sql)->fetch_all()->query_result;

            return $menu_list;
        }

        public function sort_menu($menu_list)
        {
            $parent_array = array();

            //分析一级分类
            foreach($menu_list AS $k => $v)
            {
                if ($v['parent_id'] == 0)
                {
                    $parent_array[$v['id']] = $v;
                    unset($menu_list[$k]);
                }
            }

            //分析二级分类
            foreach($menu_list AS $k => $v)
            {
                $parent_array[$v['parent_id']]['child'][] = $v;
            }

            return $parent_array;
        }

        /**
         * 通过用户权限获取菜单列表
         * @param $permissions
         */
        public function through_user_permissions_get_list($permissions, $menu_list)
        {
            switch ($permissions['permissions'])
            {
                case 'developers':

                break;
                case 'all':
                    unset($menu_list[1]);
                break;
                default:
                    unset($menu_list[1]);

                    $unset_list = explode(',', $permissions);

                    foreach($unset_list AS $v)
                    {
                        if (isset($menu_list[$v]) === FALSE)
                        {
                            unset($menu_list[$v]);
                        }
                    }
                break;
            }

            return $menu_list;
        }

        /**
         * 获取用户权限列表
         * @param $group_id
         */
        public function get_user_permissions($group_id)
        {
            $admin_group = config::get()->table->admin_group;

            $sql = "select `name`, `permissions` from `{$admin_group}` where `id` = {$group_id}";

            return $this->db->query($sql)->fetch_one()->query_result;
        }

        /**
         * 获取一个菜单的详细信息
         * @param unknown_type $id
         */
        public function get_one_menu($id)
        {
            $sql = "select * from `{$this->table_name}` where id = {$id}";
            $this->db->query($sql)->fetch_one();

            $menu_info = $this->db->query_result;

            if ($menu_info['parent_id'] == 0)
            {
                $menu_info['parent_name'] = '顶级分类';

                return $menu_info;
            }

            $sql = "select `name` from `{$this->table_name}` where id = {$menu_info['parent_id']}";
            $this->db->query($sql)->fetch_one();
            $menu_info['parent_name'] = $this->db->query_result['name'];

            return $menu_info;
        }

        /**
         * 设置菜单ID，用来识别新增和更新
         * @param unknown_type $id
         */
        public function set_id($id)
        {
            $this->id = $id;
        }

        /**
         * 保存菜单，新增和更新都是用此方法
         * @param $name
         * @param $parent_id
         * @param $filename
         * @param $sort
         */
        public function save($name, $parent_id, $filename, $sort)
        {
            if ($this->id == '')
            {
                //新增
                $sql = "insert into `{$this->table_name}`(`name`, `parent_id`, `filename`, `sort`) values('{$name}', '{$parent_id}', '{$filename}', '{$sort}')";
            }
            else
            {
                //更新
                $sql = "update `{$this->table_name}` set `name` = '{$name}', `parent_id` = '{$parent_id}', `filename` = '{$filename}', `sort` = '{$sort}' where `id` = '{$this->id}'";
            }

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 删除一个菜单
         * @param unknown_type $id
         */
        public function del($id)
        {
            $sql = "delete from `{$this->table_name}` where `id` = {$id}";

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
    }